#!/bin/bash

if [ -t 0 ]; then stty -echo -icanon -icrnl time 0 min 0; fi

rm -rf controlDDS.out

BEFORE_LINES=$(tput lines)
BEFORE_COLS=$(tput cols)

function PRINT_MENU {
    clear
    COLUMNS=$(tput cols)
    if (( ${COLUMNS} > 75 )); then
	echo -ne "\033[40m      \033[0m > r > "
	echo -ne "\033[42m       \033[0m > s > "
	echo -ne "\033[44m     \033[0m > t > "
	echo -ne "\033[46m            \033[0m > d > "
	echo -ne "\033[47m    \033[0m     |"
	echo ""
	echo -ne "\033[40mPAUSED\033[0m       "
	echo -ne "\033[42mRUNNING\033[0m       "
	echo -ne "\033[44mREADY\033[0m       "
	echo -ne "\033[46mDEVICE_READY\033[0m       "
	echo -ne "\033[47mIDLE\033[0m > q | x-Exit"
	echo ""
	echo -ne "\033[40m      \033[0m < p < "
	echo -ne "\033[42m       \033[0m < r < "
	echo -ne "\033[44m     \033[0m < j < "
	echo -ne "\033[46m            \033[0m < i < "
	echo -ne "\033[47m    \033[0m     |"
	echo ""
    fi;
}

PRINT_MENU

WAITINGMILL="\|/-"
NOFLINESTOPRINT=1
DEVICESPERLINE=0

MAX_DEV_LEN=20
MIN_DEV_LEN=12

count=0
keypress=''
while [ "$keypress" != "x" ]; do
  let count+=1
  keypress="`cat -v`"
  LINES=$(tput lines)
  COLS=$(tput cols)
  if [ "$BEFORE_LINES" != "$LINES" ] || [ "$BEFORE_COLS" != "$COLS" ]; then
      PRINT_MENU
  fi
  if (( ${NOFLINESTOPRINT} > 1 )); then
      FIRSTLINE=0
      if (( ${COLUMNS} > 74 )); then
	  FIRSTLINE=3
      fi
      tput cup $FIRSTLINE 0
      for (( i = 0 ; i < $NOFLINESTOPRINT ; i++ )); do
	  echo -e "\033[2K";
      done
      echo " ";
      tput cup $FIRSTLINE 0
  fi
  NOFWORKERS="$(dds-info -n)"
  if [ -z "$NOFWORKERS" ]; then NOFWORKERS=0; fi
  if [ "$NOFWORKERS" -gt 0 ]; 
  then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c c | sort &> command.out
  fi
  for (( i = 0 ; i < $NOFWORKERS ; i++ )); do
      TASKS[$i]="";
  done
  NOFTASKS=0
  if [ -f command.out ]; then
      while read p; do
	  if [[ $p == *"Received"* ]]
	  then
	      TASKS[$NOFTASKS]=${p}
	      let NOFTASKS+=1
	  fi
      done < command.out
      rm -f command.out
  fi
  echo -ne "\033[0K\r${WAITINGMILL:$((count%4)):1}"
  if (( ${NOFWORKERS} > 0 )); then
    TERMINALCOLUMNS=$(tput cols)
    DEVICESPERLINE=0
    NOFLINESTOPRINT=1
    DEVICELENGTH=$((((TERMINALCOLUMNS-1) / NOFWORKERS)-2))
    if (( ${DEVICELENGTH} > ${MAX_DEV_LEN} )); then DEVICELENGTH=${MAX_DEV_LEN}; fi
    if (( ${DEVICELENGTH} < ${MIN_DEV_LEN} )); then DEVICELENGTH=${MIN_DEV_LEN};  DEVICESPERLINE=$(((TERMINALCOLUMNS-1)/(DEVICELENGTH+2))); NOFLINESTOPRINT=$((1+(NOFWORKERS/DEVICESPERLINE))); fi

    for (( i = 0 ; i < $NOFWORKERS ; i++ )); do
        TOPRINT=${TASKS[i]}
        TOPRINT=${TOPRINT% (*}
        TOPRINT=${TOPRINT%:*}
        TOPRINT=${TOPRINT#*: }
	TASTATE=${TASKS[i]}
	TASTATE=${TASTATE% (*}
        TASTATE=${TASTATE#*: }
        TASTATE=${TASTATE#*: }
	
	echo -ne "["
	if [[ $TASTATE == "PAUSED" ]]; then echo -ne "\033[40m"; fi
	if [[ $TASTATE == "RUNNING" ]]; then echo -ne "\033[42m"; fi
	if [[ $TASTATE == "READY" ]]; then echo -ne "\033[44m"; fi
	if [[ $TASTATE == "DEVICE READY" ]]; then echo -ne "\033[46m"; fi
	if [[ $TASTATE == "IDLE" ]]; then echo -ne "\033[47m"; fi
	if (( ${#TOPRINT} > ${DEVICELENGTH} )); then
	    TOPRINT="${TOPRINT:0:$((DEVICELENGTH+1-MIN_DEV_LEN))}~${TOPRINT: 2-MIN_DEV_LEN}"
	fi
	printf  "%${DEVICELENGTH}s" $TOPRINT
	echo -ne "\033[0m"
	echo -ne "]"
	if (( ${NOFLINESTOPRINT} > 1 )); then
	    if (( $((i%DEVICESPERLINE)) == $((DEVICESPERLINE-1)) )); then echo ""; echo -n " "; fi;
	fi;
    done
  fi
  echo -ne "\r"
  if [ "$keypress" == "p" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c p | sort &> command.out
  fi
  if [ "$keypress" == "s" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c s | sort &> command.out
  fi
  if [ "$keypress" == "t" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c t | sort &> command.out
  fi
  if [ "$keypress" == "d" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c d | sort &> command.out
  fi
  if [ "$keypress" == "r" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c r | sort &> command.out
  fi
  if [ "$keypress" == "j" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c j | sort &> command.out
  fi
  if [ "$keypress" == "i" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c i | sort &> command.out
  fi
  if [ "$keypress" == "q" ]; then
      tail -1 ~/.DDS/default.sid | xargs -I {} $SIMPATH/bin/fairmq-dds-command-ui -s {} -c q | sort &> command.out
  fi
  if [ "$keypress" == "x" ]; then
      break;
  fi
  sleep 1
done

if [ -t 0 ]; then stty sane; fi

echo ""
echo "Thank you for using this script (for $count seconds)."
exit 0
